package org.devcp.tp.common.websocket;

import java.util.Map;

import org.apache.log4j.Logger;
import org.jweb.core.util.StringUtil;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.alibaba.fastjson.JSONArray;

/**
 * 喊单websocket拦截器，将当前用户喊单设置信息存放到websocket session中
 * @author Administrator
 *
 */
public class NotifyWebSocketHandshakeInterceptor implements HandshakeInterceptor {
	private static final Logger logger = Logger.getLogger(NotifyWebSocketHandshakeInterceptor.class);
	
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object
                > attributes) throws Exception {
    	logger.debug("running beforeHandshake method...");
    	
    	if(request.getHeaders().containsKey("Sec-WebSocket-Extensions")) {
    		request.getHeaders().set("Sec-WebSocket-Extensions", "permessage-deflate");
       }
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            String ulistJsonStr = servletRequest.getServletRequest().getParameter("ulist");
            if(StringUtil.isNotEmpty(ulistJsonStr)){
            	attributes.put(Constants.USER_NOTIFY_SETTING,ulistJsonStr);
            }
//            
        }
        return true;
    }
// 
//    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
    	
    }
}

